#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：python常用模块库 
@File ：main.py
@IDE  ：PyCharm 
@Author ：李涵彬
@Date ：2025/2/21 上午8:59 
"""

import platform
import sys
from pathlib import Path
from typing import Any

from file_monitor.utils.config import ConfigManager
from file_monitor.utils.logger import setup_logging


def main():
	# 初始化配置
	config_path = Path(__file__).parent / "config.ini"
	config = ConfigManager(config_path)

	# 配置日志
	setup_logging(config)

	# 选择平台实现
	current_platform = platform.system().lower()
	if current_platform == "linux":
		from services.daemon import LinuxDaemon
		service = LinuxDaemon(config)
	elif current_platform == "windows":
		from services.winservice import WinService
		service = WinService(config)
	else:
		raise RuntimeError(f"Unsupported platform: {current_platform}")

	# 处理命令行参数
	if len(sys.argv) > 1:
		handle_cli_commands(sys.argv[1:], service)
	else:
		service.start()


def handle_cli_commands(args: list, service: Any):
	"""处理命令行参数"""
	cmd = args[0].lower()
	if cmd == "start":
		service.start()
	elif cmd == "stop":
		service.stop()
	elif cmd == "restart":
		service.stop()
		service.start()
	elif cmd == "status":
		print(service.status())
	else:
		print(f"Unknown command: {cmd}")
		sys.exit(1)


if __name__ == "__main__":
	main()
